import { createApp } from 'vue'
import BaseToast from '@/components/BaseToast.vue'

let toastInstance

function init() {
    if (!toastInstance) {
        const mountNode = document.createElement('div')
        document.body.appendChild(mountNode)
        toastInstance = createApp(BaseToast).mount(mountNode)
    }
}

export default {
    success(msg, duration = 2500) {
        init()
        toastInstance.show(msg, 'success', duration)
    },
    error(msg, duration = 2500) {
        init()
        toastInstance.show(msg, 'error', duration)
    },
    warning(msg, duration = 2500) {
        init()
        toastInstance.show(msg, 'warning', duration)
    },
    info(msg, duration = 2500) {
        init()
        toastInstance.show(msg, 'info', duration)
    }
}
